export function sedMsg(type,data){
	localStorage.setItem('@@'+type,JSON.stringify({
		data,
		temp:Date.now()
	}) )
}


export function getMsg(handler){
	const storageHadler = e =>{
		const data = JSON.parse(e.newValue)
		handler(e.key.substring(2),data.data)
	}
	window.addEventListener('storage',storageHadler)
	return ()=>{
		window.removeEventListener('storage',storageHadler)
	}
}

export function Storage(type) {
	return window.dispatchEvent(new StorageEvent('storage', {
		key: '@@scancode',
		newValue: localStorage.getItem('@@scancode'),
		url: 'http://192.168.8.27:8080'
	}));
}

const channel = new BroadcastChannel('channel')

export function giveMsg(type,content){
	channel.postMessage({
		type,
		content
	}) 
}

export function listenMsg(callback){
	channel.addEventListener('message',(e)=>{
		callback && callback(e.data)
	})
}

